// import xlsx from "xlsx";
const xlsx = require('xlsx')
// 第一个表
const workbook = xlsx.readFile('./source/示例表格.xlsx');  // 获取excel文件对象 as: Object
const sheetNames = workbook.SheetNames; // 获取excel文件中所有的工作表名字 as: Array
// console.log('SheetNames:', sheetNames) // [ 'Sheet1' ]
const worksheet = workbook.Sheets[sheetNames[0]];  // 'Sheet1'表对象 as: Object
// console.log('worksheet:', worksheet)
const range = xlsx.utils.decode_range(worksheet['!ref']);  // 创建一个数据范围对象 as: Object
// console.log('range:', range1)  // { s: { c: 0, r: 0 }, e: { c: 3, r: 4 } }

//循环获取单元格值
let row_value = '';
for (let R = range.s.r; R <= range.e.r; ++R) {
  for (let C = range.s.c; C <= range.e.c; ++C) {
    const cell_address = { c: C, r: R }; //获取单元格地址
    const cell = xlsx.utils.encode_cell(cell_address); //根据单元格地址获取单元格
    //获取单元格值
    if (worksheet[cell]) {
      // 如果出现乱码可以使用iconv-lite进行转码  iconv-lite: 字符串解码工具 安装: npm i iconv-lite
      // row_value += iconv.decode(worksheet[cell].v, 'gbk') + ", ";
      row_value += worksheet[cell].v + ", ";
    } else {
      row_value += ", ";
    }
  }
  row_value += "\r\n";
}
console.log(row_value);
